package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.admin.AdminConstans;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mappers.ApUserMapper;
import com.usian.user.mappers.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    ApAuthorFeign articleFeign;
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    ApUserMapper userMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null) return errorParam;
        return getPageData(dto, Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus()));
    }

    //认证表，自媒体人表，作者表，用户表
    //认证表，用户表做更新
    //自媒体人表，作者表做插入
    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        if (dto == null || dto.getId() == null) return errorParam;
        //用户ID
        ApUserRealname attestation = getById(dto.getId());
        //用户ID查出用户对象 c端用户
        ApUser user_c = userMapper.selectById(attestation.getUserId());
        ApUserRealname realname = new ApUserRealname();
        realname.setStatus(status);
        realname.setId(dto.getId());
        //修改状态
        if (StringUtils.isNotEmpty(dto.getMsg())) realname.setReason(dto.getMsg());
        updateById(realname);
        if (status == AdminConstans.PASS_AUTH) {
            //如果审核通过需要将数据插入倒自媒体人表和作者表中
            WmUser WmUser = wemediaFeign.findByName(user_c.getName());
            if (WmUser == null) {
                WmUser = new WmUser();
                //从上面用户信息中获取到用户的id
                WmUser.setApUserId(attestation.getUserId());
                //通过字段克隆给自媒体表中
                BeanUtils.copyProperties(user_c, WmUser);//字段同步
                WmUser.setCreatedTime(new Date());
                WmUser.setStatus(9);
                //克隆完成之后远程调用方法保存
               ResponseResult save= wemediaFeign.save(WmUser);
                LinkedHashMap map =(LinkedHashMap) save.getData();
               WmUser.setId((Integer)map.get("id"));
            }
            ApAuthor apAuthor = articleFeign.findByUserId(user_c.getId());//找到作者
            if (apAuthor == null) {
                ApAuthor author = new ApAuthor();
                author.setName(user_c.getName());
                author.setUserId(user_c.getId());
                author.setCreatedTime(new Date());
                    author.setWmUserId(WmUser.getId());
                author.setType(2);
               ResponseResult result= articleFeign.save(author);
                LinkedHashMap map =(LinkedHashMap) result.getData();
               WmUser.setApAuthorId((Integer)map.get("id"));
               wemediaFeign.updateAuthorId(WmUser);
            }
            //更新flag
            ApUser tempUser = new ApUser();
            tempUser.setId(user_c.getId());
            tempUser.setFlag((short) 1);
            userMapper.updateById(tempUser);

        }
        int a=1/0;
        return success;

    }
}
